<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_date".
 *
 * @property int $id
 * @property int $tid
 * @property string $date
 * @property int $inventory
 * @property string $ori_price
 * @property string $ruling_price
 * @property string $update_auth
 * @property string $state
 * @property string $create_time
 */
class TicketDate extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_date';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['tid', 'date', 'inventory', 'ori_price', 'ruling_price'], 'required'],
            [['tid', 'inventory'], 'integer'],
            [['ori_price', 'ruling_price'], 'number'],
            [['state'], 'string'],
            [['create_time'], 'safe'],
            [['date'], 'string', 'max' => 10],
            [['update_auth'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tid' => 'Tid',
            'date' => 'Date',
            'inventory' => 'Inventory',
            'ori_price' => 'Ori Price',
            'ruling_price' => 'Ruling Price',
            'update_auth' => 'Update Auth',
            'state' => 'State',
            'create_time' => 'Create Time',
        ];
    }


    public function getTicketDateInfo($tid)
    {
        $data = self::find()->where('tid = :id',[':id' => $tid])->asArray()->all();
        return $data;
    }


    /*** 设置门票日期库存，价格
     * @param $auth
     * @param $params
     * @return int
     */
    public function setTicketDateInfo($auth,$params)
    {
//        $params = [
//            'tid' => 2,
//            'list' => [
//                [
//                    'date' => '2020-08-26',
//                    'inventory' => 20,
//                    'ori_price' => 268,
//                    'ruling_price' => 268,
//                    'update_auth' => $auth,
//                    'state' => '1',
//                ]
//            ]
//        ];
        if  (isset($params['tid']) and !empty($params['tid'])){
            $data['TicketDate']['tid'] = $params['tid'];
        }else{
            $this->addError('tid','门票id不能为空');
        }
        if (!empty($params['list']) and is_array($params['list'])){
            $ok = 0;
            foreach ($params['list'] as $key => $val){
                $model = new self();
                $dateData = [
                    'TicketDate' => [
                        'tid' => $data['TicketDate']['tid'],
                        'date' => $val['date'],
                        'inventory' => $val['inventory'],
                        'ori_price' => $val['ori_price'],
                        'ruling_price' => $val['ruling_price'],
                        'update_auth' => empty($auth)?'':$auth,
                        'state' => isset($val['state'])?$val['state']:'1',
                        'create_time' => date("Y-m-d H:i:s"),
                    ]
                ];

                if ($model->addTicketDate($dateData)){
                    $ok++;
                }
            }
        }
        return $ok;








    }

    /*** 添加、修改门票库存
     * @param $data
     * @return bool
     */
    private function addTicketDate($data)
    {
        if (!empty($data['TicketDate']['date']) and !empty($data['TicketDate']['tid'])){
            $info = self::find()->where(['date' => $data['TicketDate']['date'],'tid' => $data['TicketDate']['tid']])->select(['id'])->one();
            if  (!is_null($info)){
                if ($this->modTicketDate($info->id,$data)){
                    return true;
                }
                return false;
            }
        }

        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }

    /*** 修改单个日期门票的库存
     * @param $id
     * @param $data
     * @return bool
     */
    public function modTicketDate($id,$data)
    {
        if ($this->load($data) and $this->validate()){
            $model = self::find()->where('id = :did',[':did' => $id])->one();
            $model->tid = $data['TicketDate']['tid'];
            $model->date = $data['TicketDate']['date'];
            $model->inventory = $data['TicketDate']['inventory'];
            $model->ori_price = $data['TicketDate']['ori_price'];
            $model->ruling_price = $data['TicketDate']['ruling_price'];
            $model->update_auth = $data['TicketDate']['update_auth'];
            $model->state = $data['TicketDate']['state'];
            if ($model->save(0)){
                return true;
            }
        }
        return false;
    }






}
